﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PSPmmo.LoginServer.Network
{
    class Packet
    {
        string opcode { get; set; }
        string data { get; set; }

        public Packet(byte[] rec_bytes, int length)
        {
            ASCIIEncoding encoder = new ASCIIEncoding();
            string msg = encoder.GetString(rec_bytes, 0, length + 1);

            Console.WriteLine(msg);

            try
            {
                char[] opcode = new char[4];
                char[] data = new char[msg.Length - 4];

                int i = 0;
                while (i < 4)
                {
                    opcode[i] = msg[i]; i++;
                }

                int j = 0;

                while (i < msg.Length)
                {
                    data[j] = msg[i]; i++; j++;
                }

                this.opcode = new string(opcode);
                this.data = new string(data);
            }
            catch (OverflowException)
            {
                return;
            }
        }

        public Opcodes GetOpcode()
        {
            try
            {
                return (Opcodes)Convert.ToInt32(opcode.ToString());
            }
            catch (FormatException)
            {
                return Opcodes.UNK;
            }
        }

        public string GetData()
        {
            return data.ToString();
        }
    }
}
